package com.lagou.hdfs;

/**
 * @ClassName MyProcessor
 * @Description TODO
 * @Author xsq
 * @Date 2020/10/13 10:58
 **/

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTableWrapper;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class MyProcessor extends BaseRegionObserver {
    @Override
    public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, final Delete delete, WALEdit edit, Durability durability) throws IOException {
        HTableWrapper userRel = (HTableWrapper) e.getEnvironment().getTable(TableName.valueOf("user"));
        //获取删除的行rowkey
        byte[] row = delete.getRow();
        //获取到firends下所有的cell
        List<Cell> friends = delete.getFamilyCellMap().get(Bytes.toBytes("friends"));
        Cell friend = friends.get(0);
        byte[] bytes = CellUtil.cloneQualifier(friend);
        // 组装delete操作
        Delete delete1 = new Delete(bytes);
        delete1.addColumn(Bytes.toBytes("friends"), row);
        userRel.delete(delete1);
        userRel.close();
    }

}
